package com.osdp.sc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.common.constant.CommonConstant;
import com.osdp.common.util.ConvertUtil;
import com.osdp.sc.dto.RoleDTO;
import com.osdp.sc.entity.ScRole;
import com.osdp.sc.mapper.ScRoleMapper;
import com.osdp.sc.service.IScRoleService;
import com.osdp.sc.vo.ScRoleVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.osdp.sc.uitl.Fields.ScRoleQ;

@Service
public class ScRoleServiceImpl extends ServiceImpl<ScRoleMapper, ScRole> implements IScRoleService {

    @Autowired
    private ScRoleMapper scRoleMapper;

    @Override
    public IPage<ScRole> pageList(ScRole role, Page<ScRole> page) {
        return super.page(page, new QueryWrapper<ScRole>()
                .like(StringUtils.isNotBlank(role.getRoleName()), ScRoleQ.roleName, role.getRoleName())
                .like(StringUtils.isNotBlank(role.getRoleCode()), ScRoleQ.roleCode, role.getRoleCode())
                .orderByDesc(ScRoleQ.createTime));
    }

    @Override
    public List<ScRole> queryAllRole(String roleName, String roleCode) {
        return this.list(new QueryWrapper<ScRole>()
                .like(StringUtils.isNotBlank(roleName), ScRoleQ.roleName,roleName)
                .like(StringUtils.isNotBlank(roleCode), ScRoleQ.roleCode,roleCode)
                .eq(ScRoleQ.validation,CommonConstant.FLAG_ONE_1)
        );
    }

    @Override
    public List<ScRoleVo> queryRolesByUserId(String userId){
        List<ScRole> list =  this.scRoleMapper.queryRolesByUserId(userId);
        return ConvertUtil.sourceToTarget(list,ScRoleVo.class);
    }

    @Override
    public List<ScRoleVo> queryRolesByPositionId(String positionId) {
        List<ScRole> list = scRoleMapper.queryRolesByPositionId(positionId);
        return ConvertUtil.sourceToTarget(list,ScRoleVo.class);
    }

    @Override
    @Transactional
    public boolean insertRole(ScRole entity) {
        return this.save(entity);
    }

    @Override
    @Transactional
    public boolean updateRole(ScRole entity) {
        return this.updateById(entity);
    }

    @Override
    @Transactional
    public boolean deleteRole(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean deleteBatcah(List<String> idList) {
        return this.removeByIds(idList);
    }

    /** -------------------------用于登录 ----------------------*/
    @Override
    public List<RoleDTO> getRolesByUserId(String userId) {
        List<ScRole> list =  this.scRoleMapper.queryRolesByUserId(userId);
        return ConvertUtil.sourceToTarget(list, RoleDTO.class);
    }

    @Override
    public List<String> getRoleCodeByUserId(String userId){
        List<ScRole> list =  this.scRoleMapper.queryRolesByUserId(userId);
        return list.stream().map(ScRole::getRoleCode).filter(e->StringUtils.isNotEmpty(e)).collect(Collectors.toList());
    }
//
//    @Override
//    public String saveTenantAdminRole(String name, String tenantId){
//        ScRole scRole = new ScRole();
//        scRole.setDescription("租户" + name + "管理员");
//        scRole.setRoleName("ADMIN_TENANT");
//        scRole.setRoleCode("ADMIN_TENANT_" + new Random().nextInt(999999));
//        scRole.setTenantId(tenantId);
//        this.save(scRole);
//        scPermissionService.permissionCopy(tenantId,scRole.getId());
//        return scRole.getId();
//    }
//    /**
//     *
//     * @param entity
//     * @return
//     */
//    @Override
//    public boolean updateById(ScRole entity) {
//        UserVo user = CurrentUserInfoService.getUserInfo();
//        if(!user.getUsername().equalsIgnoreCase(CommonConstant.SUPER_ADMIN)){
//            ScRole role = this.getById(entity.getId());
//            if(!role.getCreateBy().equalsIgnoreCase(user.getUsername())){//只有自己创建的角色才可以修改和删除
//                throw new IllegalArgumentException("非本人创建角色，无权限修改！");
//            }
//        }
//        return super.updateById(entity);
//    }
//
//    @Override
//    public boolean removeById(Serializable id) {
//        UserVo user = CurrentUserInfoService.getUserInfo();
//        if(!user.getUsername().equalsIgnoreCase(CommonConstant.SUPER_ADMIN)){
//            ScRole role = this.getById(id);
//            if(!role.getCreateBy().equalsIgnoreCase(user.getUsername())){//只有自己创建的角色才可以修改和删除
//                throw new IllegalArgumentException("非本人创建角色，无权限删除！");
//            }
//        }
//        return super.removeById(id);
//    }
//
//    @Override
//    public boolean removeByIds(Collection<? extends Serializable> idList) {
//        UserVo user = CurrentUserInfoService.getUserInfo();
//        if(!user.getUsername().equalsIgnoreCase(CommonConstant.SUPER_ADMIN)) {
//            LambdaQueryWrapper<ScRole> query = new LambdaQueryWrapper<ScRole>();
//            query.eq(ScRole::getDelFlag, CommonConstant.ZERO_FLAG_0);
//            query.in(ScRole::getId, idList);
//            List<ScRole> roleList = this.list(query);
//            if (roleList.size() > 0){
//                roleList.stream().forEach(role -> {
//                    if (!role.getCreateBy().equalsIgnoreCase(user.getUsername())) {//只有自己创建的角色才可以修改和删除
//                        throw new IllegalArgumentException("非本人创建角色，无权限删除！");
//                    }
//                });
//            }
//        }
//        return this.removeByIds(idList);
//    }
}
